import Vue from 'vue'
import I18N_RESOURCES_EN from 'vue-cesium/lang/en-us.js'
import I18N_RESOURCES_ZH from 'vue-cesium/lang/zh-hans.js'

const CESIUM_ION_ACCESS_TOKEN = process.env.CESIUM_ION_ACCESS_TOKEN
let token = /* 临时测试可使用如下token： */
    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiMDhkYTczYS1iMjhiLTQ1YjQtYWI4ZS1lZjFkMzRmN' +
    'zU5YzQiLCJpZCI6NDQ0MDMsImlhdCI6MTYxNzk2MDA5N30.H55MYwd31wjvNKmYZmNdk2VtPRy9hujwrnnxgVXmPow'
if (CESIUM_ION_ACCESS_TOKEN) {
    token = CESIUM_ION_ACCESS_TOKEN
}

const DEFAULT_LANGUAGE = 'en'
const locales = {
    // 'en-US': I18N_RESOURCES_EN,
    'en': I18N_RESOURCES_EN,
    // 'zh-CN': I18N_RESOURCES_ZH,
    'zh': I18N_RESOURCES_ZH,
}

function getAcceptableFromSupported(hint, locales) {
    let str = hint
    while (str.length) {
        // console.log(" str = " + str)
        if (locales[str]) {
            return str
        }
        str = str.substring(0, str.lastIndexOf('-'))
    }
    return ''
}

let language = navigator.language
    || navigator.userLanguage
    || DEFAULT_LANGUAGE
for (let i in navigator.languages) {
    if (!navigator.languages.hasOwnProperty(i)) {
        continue
    }
    let hint = navigator.languages[i]
    if (!hint) {
        continue
    }
    language = getAcceptableFromSupported(hint, locales)
    if (language) {
        break
    }
}

if (!language) {
    language = DEFAULT_LANGUAGE
}

// locales
let i18nResources = locales[language]
/* Fallback safe */
if (!i18nResources) {
    i18nResources = I18N_RESOURCES_ZH
}

if (!CESIUM_LIB_URL) {
    console.log('ERROR: Webpack打包脚本没有定义常量CESIUM_LIB_URL!')
}
let defaultCesiumLibUrl = CESIUM_LIB_URL || 'https://unpkg.com/cesium/Build/Cesium/Cesium.js'
console.log('CESIUM_LIB_URL="' + CESIUM_LIB_URL + '"')

const VC_VIEWER_OPTIONS = {
    accessToken: token,
    lang: i18nResources,
    cesiumPath: defaultCesiumLibUrl,
}

import { Viewer, ImageryLayer, WebMapTileServiceImageryProvider, VcNavigation } from 'vue-cesium'
import { TiandituImageryProvider } from 'vue-cesium'
import {
    CesiumTerrainProvider,
} from 'vue-cesium'

Vue.use(Viewer, VC_VIEWER_OPTIONS)
Vue.use(ImageryLayer)
Vue.use(WebMapTileServiceImageryProvider)
Vue.use(VcNavigation)
Vue.use(TiandituImageryProvider)
Vue.use(CesiumTerrainProvider)
// Vue.use(ArcGISTiledElevationTerrainProvider)// FIXME: ArcGISTiledElevationTerrainProvider地形提供者组件存在缺陷非常卡顿
